﻿/*
 * File: MenuWindow.cs
 * Developer : Brent Larsen (brentoboy@gmail.com)
 * Date: 2011-AUG-9
 * Copyright (c) 2011 Brent Larsen.
 * Distributed under the terms of the GPL v3
 *  
 * This file is part of GenX-RPG
 * http://code.google.com/p/genx-rpg/ 
 * 
 * GenX-RPG is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GenX-RPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GenX-RPG.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Rpg.Editor
{
    public partial class MenuWindow : Form
    {
        public MenuWindow()
        {
            InitializeComponent();
        }

        public MenuWindow(string dummy)
            :this()
        {
            foreach (var kvp in AdventureDatabase.GetAdventureList())
            {
                var menuItem = new ToolStripMenuItem();
                menuItem.Name = "menuAdventure_Open_{0}".f(kvp.Key.Replace(".genx", ""));
                menuItem.Text = kvp.Value;
                menuItem.Tag = kvp.Key;
                menuItem.Click += new EventHandler(menuAdventure_Open_Click);
                this.menuAdventures.DropDownItems.Insert(2, menuItem);
            }

            foreach (var kvp in TileSetDatabase.GetTileSetList())
            {
                var menuItem = new ToolStripMenuItem();
                menuItem.Name = "menuTileSet_Open_{0}".f(kvp.Key.Replace(".tiles", ""));
                menuItem.Text = kvp.Value;
                menuItem.Tag = kvp.Key;
                menuItem.Click += new EventHandler(menuTileSet_Open_Click);
                this.menuTileSets.DropDownItems.Insert(2, menuItem);
            }
        }

        private void menuFileExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuTileSet_CreateNew_Click(object sender, EventArgs e)
        {
            using (var dialog = new OpenFileDialog())
            {
                dialog.CheckPathExists = true;
                dialog.CheckFileExists = true;
                dialog.DereferenceLinks = true;
                dialog.Filter = "Bitmaps (*.bmp)|*.bmp";
                dialog.FilterIndex = 1;
                dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                dialog.Multiselect = false;
                dialog.ValidateNames = true;
                dialog.Title = "Bitmap For The Tile Set...";

                if (dialog.ShowDialog() != DialogResult.Cancel
                    && File.Exists(dialog.FileName))
                {
                    var db = new TileSetDatabase(new FileInfo(dialog.FileName));
                    var uniqueId = db.GetUniqueId();

                    var menuItem = new ToolStripMenuItem();
                    menuItem.Name = "menuTileSet_Open_" + uniqueId;
                    menuItem.Text = db.GetTileSetName();
                    menuItem.Tag = uniqueId + ".tiles";
                    menuItem.Click += new EventHandler(menuTileSet_Open_Click);
                    this.menuTileSets.DropDownItems.Insert(3, menuItem);

                    var window = new TileSetWindow(db);
                    window.Show(this);
                }
            }
        }

        private void menuTileSet_Open_Click(object sender, EventArgs e)
        {
            var menuItem = sender as ToolStripMenuItem;
            var window = new TileSetWindow(new TileSetDatabase(menuItem.Tag.ToString()));
            window.Show(this);
        }

        private void menuTileSet_Import_Click(object sender, EventArgs e)
        {
            using (var dialog = new OpenFileDialog())
            {
                dialog.CheckPathExists = true;
                dialog.CheckFileExists = true;
                dialog.DereferenceLinks = true;
                dialog.Filter = "GenX RPG Tilesets (*.tiles)|*.tiles";
                dialog.FilterIndex = 1;
                dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                dialog.Multiselect = false;
                dialog.ValidateNames = true;
                dialog.Title = "Import Tile Set...";

                if (dialog.ShowDialog() != DialogResult.Cancel
                    && File.Exists(dialog.FileName))
                {
                    var kvp = TileSetDatabase.Import(new FileInfo(dialog.FileName));
                    if (kvp != null)
                    {
                        var fileName = kvp.Value.Key;
                        var uniqueId = fileName.Replace(".tiles", "");
                        var tilesetName = kvp.Value.Value;
                        var menuItemName = "menuTileSet_Open_" + uniqueId;

                        var menuItem = menuTileSets.DropDownItems.OfType<ToolStripMenuItem>().Where(mi => mi.Name == menuItemName).SingleOrDefault();
                        if (menuItem == null)
                        {
                            menuItem = new ToolStripMenuItem();
                            menuItem.Name = menuItemName;
                            menuItem.Tag = fileName;
                            menuItem.Click += new EventHandler(menuTileSet_Open_Click);
                            this.menuTileSets.DropDownItems.Insert(3, menuItem);
                        }
                        menuItem.Text = tilesetName;
                    }
                }
            }
        }

        private void menuAdventure_CreateNew_Click(object sender, EventArgs e)
        {
            var name = Microsoft.VisualBasic.Interaction.InputBox("Adventure Name...", "New Adventure", "Adventure");

            if (!name.IsBlank())
            {
                var folderName = name.Replace(" ", "-").ToLower();

                var menuItem = new ToolStripMenuItem();
                menuItem.Name = "menuAdventure_Open_{0}".f(folderName.Replace(".", "_"));
                menuItem.Text = "{0} ({1})".f("New Adventure", folderName);
                menuItem.Tag = folderName;
                menuItem.Click += new EventHandler(menuAdventure_Open_Click);
                this.menuAdventures.DropDownItems.Insert(3, menuItem);

                var window = new AdventureWindow(new AdventureDatabase(menuItem.Tag.ToString()));
                window.Show(this);
            }
        }

        private void menuAdventure_Open_Click(object sender, EventArgs e)
        {
            var menuItem = sender as ToolStripMenuItem;
            var window = new AdventureWindow(new AdventureDatabase(menuItem.Tag.ToString()));
            window.Show(this);
        }

        private void menuAdventure_Import_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        //private void menuFile_AdventureProperties_Click(object sender, EventArgs e)
        //{
        //    var menuItem = sender as ToolStripMenuItem;
        //    new AdventureEditWindow(new AdventureDatabase(menuItem.Tag.ToString()).Show();
        //}

        //private void menuMap_New_Click(object sender, EventArgs e)
        //{
        //    var window = new MapEditWindow(null);
        //    window.Show(this);
        //}

        //internal void menuMap_Edit_Click(object sender, EventArgs e)
        //{
        //    var menuItem = sender as ToolStripMenuItem;
        //    var window = new MapEditWindow((int)menuItem.Tag);
        //    window.Show(this);
        //}

        //private void _loadAdventure(AdventureDatabase db)
        //{
        //    Program.Db = db;

        //    // update the caption for the window
        //    this.Text = "Adventure Editor - {0}".f(db.GetAdventureName());

        //    // throw out maps that were loaded previously
        //    var oldMaps = this.menuMap.DropDownItems.OfType<ToolStripMenuItem>().Where(menuItem => menuItem.Tag != null).ToList();
        //    foreach (var map in oldMaps)
        //    {
        //        this.menuMap.DropDownItems.Remove(map);
        //    }

        //    // add maps for this database
        //    foreach (var map in db.GetMapNames())
        //    {
        //        var menuItem = new ToolStripMenuItem();
        //        menuItem.Name = "menuMap_Edit{0}".f(map.Key);
        //        menuItem.Text = map.Value;
        //        menuItem.Tag = map.Key;
        //        menuItem.Click += new EventHandler(menuMap_Edit_Click);
        //        this.menuMap.DropDownItems.Add(menuItem);
        //    }

        //    // enable the Add New Map button
        //    menuMap_New.Enabled = true;
        //}

    }
}
